import Vue from 'vue'
import Router from 'vue-router'
import Login from '@/components/Login'
import Home from '@/components/Home'
import index from '@/components/index'
import spike from '@/components/comm/spike'
import com from '@/components/comm/com'
import userAddress from '@/components/user/userAddress'
import userCollect from '@/components/user/userCollect'
import userShop from '@/components/user/userShop'
import userShoppingCart from '@/components/user/userShoppingCart'
import Test from '@/components/Test'
import coupon from '@/components/coupon/coupon'
import search from '@/components/search'

Vue.use(Router)

const router= new Router({
  routes: [
    { path: '/', redirect:'/index'},
    { path: '/login', component: Login},
    { path: '/index', component: index},
    { path: '/spike', component: spike},
    { path: '/test', component: Test},
    { path: '/com', component: com},
    { path: '/search', component: search},
    { path: '/coupon', component: coupon},
    { path: '/user/userAddress', component: userAddress},
    { path: '/user/userCollect', component: userCollect},
    { path: '/user/userShop', component: userShop},
    { path: '/user/userShoppingCart', component: userShoppingCart},
    {path: '/home',component: Home}
  ]
})

//挂载 路由守卫
router.beforeEach((to, from, next) => {
   //to 将要 访问的路径
   //from 代表从那个路径跳转而来的
   //next 是一个函数 表示放行
   // next()  放行 next("/login") 强制跳转

   //判断是不是登录页面
   if(to.path==='/login')return next()
   if(to.path==='/index')return next()
   if(to.path==='/spike')return next()
    //获取token
   const tokenStr = window.sessionStorage.getItem('token')
  //判断是不是合格的token
/*   if (!tokenStr) return next('/login')
  */
  //有token就直接放行
  next()

}) 

export default router
